﻿(function () {
    sqlite = {
        settings: {
            fileName: ''
        },

        open: function (fileName) {
            sqlite.settings.fileName = fileName;
            return this;
        },

        exec: function (sql, params, callback) {
            var arr = [sqlite.settings.fileName];
            var _callback = null;
            for (var i = 0; i < arguments.length; i++) {
                var arg = arguments[i];
                if (typeof (arg) == 'function') {
                    _callback = arg;
                } else {
                    arr.push(arg);
                }
            }

            jDyn.invoke({
                func: 'sqlite',
                args: arr,
                success: function (data) {
                    if (_callback != null) {
                        _callback(eval('(' + data + ')'));
                    }
                },
                error: function (xmlHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown.message);
                }
            });

            return this;
        }
    };

    window.sqlite = sqlite;
})();
